package com.mz.ozy.activity.login;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.RegisterPresenter;
import com.mz.ozy.mvp.view.RegisterContract;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FinishRegisterActivity extends BaseActivity<RegisterContract.View, RegisterPresenter> implements RegisterContract.View {


    @BindView(R.id.nickNameEt)
    EditText nickNameEt;
    @BindView(R.id.loginPwd)
    EditText loginPwd;
    @BindView(R.id.payPwdEt)
    EditText payPwdEt;

    private String userOne;

    private String code;

    public static Activity lastActivity;
    private String tjr;

    public static void go(Activity context, String username, String code, String yjr) {
        lastActivity = context;
        Intent intent = new Intent(context, FinishRegisterActivity.class);
        intent.putExtra("userOne", username);
        intent.putExtra("code", code);
        intent.putExtra("tjr", yjr);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finish_register);
        ButterKnife.bind(this);
        initTitle();
        initData();
        tvCenterTitle.setText(R.string.account_register);
    }

    private void initData() {
        final Intent intent = getIntent();
        userOne = intent.getStringExtra("userOne");
        code = intent.getStringExtra("code");
        tjr = intent.getStringExtra("tjr");
    }

    @OnClick({R.id.bt_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_register:
                obtainRegister();
                break;
        }
    }

    private void obtainRegister() {
        //登录密码
        String loginPwd1 = loginPwd.getText().toString().trim();
        String payPwd = payPwdEt.getText().toString().trim();
        String nickName = nickNameEt.getText().toString().trim();
        if (TextUtils.isEmpty(loginPwd1) || loginPwd.length() < 8 || !Utils.ispsd(loginPwd1)) {
            ToastUtils.showToast(R.string.str_input_newpwd_hint);
            return;
        }
        if (TextUtils.isEmpty(payPwd) || payPwd.length() != 6) {
            ToastUtils.showToast(getString(R.string.str_paypwd_format_hint));
            return;
        }
        if (TextUtils.isEmpty(nickName)) {
            ToastUtils.showToast(getString(R.string.hint_user));
            return;
        }

        mPresenter.obtainRegister(this, nickName, userOne, code, loginPwd1, payPwd, tjr);
    }

    @Override
    public void getCodeSuccess(Object o) {
    }

    @Override
    public void getImageCodeInfo(Bitmap bitmap) {

    }

    @Override
    public void obtainRegisterSuccess(Object o) {
        ToastUtils.showToast(getString(R.string.str_register_success));
        SPUtils.getInstance(this).putString(Constants.SPName.USERNAME, nickNameEt.getText().toString().trim());
        finish();
        lastActivity.finish();
        startActivity(new Intent(this, LoginActivity.class));
    }
}
